﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DunSilverlight.Comm;
using System.Windows.Browser;

namespace DunSilverlight.Views
{
    public partial class LoginForm : UserControl
    {
        private bool isSuccess;
        //private DunLibrary.User.User user;

        public LoginForm()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(LoginForm_Loaded);
        }

        public bool IsSuccess
        {
            get
            {
                return isSuccess;
            }
        }
        /// <summary>
        /// 当前登录的用户
        /// </summary>
        public DunLibrary.User.User LoginUser { get; set; }
        /// <summary>
        /// 当前登录用户的权限
        /// </summary>
        public IList<DunLibrary.User.Right> UserRights { get; set; }
        /// <summary>
        /// 回调函数
        /// </summary>
        public Action CallBack { get; set; }

        void LoginForm_Loaded(object sender, RoutedEventArgs e)
        {
            HtmlPage.Plugin.Focus();
            
            txtName.Focus(); 
        }
        /// <summary>
        /// 登录用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            login();

        }

        private void login()
        {
            //检查用户名和口令
            if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                Tools.ShowMessage("请录入用户名!", string.Empty, false);
                return;
            }

            if (string.IsNullOrEmpty(txtPass.Password.Trim()))
            {
                Tools.ShowMessage("请录入口令!", "本系统不允许空口令!", false);
                return;
            }


            UserService.UserServiceClient ser1 = new UserService.UserServiceClient();
            //登录完成的事件
            ser1.LoginCompleted += (object sender1, UserService.LoginCompletedEventArgs e1) =>
            {
                LoginUser = JsonSerializerHelper.JsonToEntity<DunLibrary.User.User>(e1.Result);

                if (LoginUser != null)
                {
                    RightService.RightServiceClient ser2 = new RightService.RightServiceClient();

                    ser2.GetRightsByRoleIdCompleted += (object sender2, RightService.GetRightsByRoleIdCompletedEventArgs e2) =>
                    {
                        int total = 0;
                        UserRights = JsonSerializerHelper.JsonToEntities<DunLibrary.User.Right>(e2.Result, out total);
                        //这一步完成，我们才认为登录成功
                        isSuccess = true;

                        CallBack();
                        Loger.WriteLog("登录", "");

                    };
                    ser2.GetRightsByRoleIdAsync(LoginUser.Role.Id);
                }
            };
            ser1.LoginAsync(txtName.Text.Trim(), txtPass.Password.Trim());//异步调用服务器端登录
        }
        /// <summary>
        /// 退出登录界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            ScriptObject Show = HtmlPage.Window.GetProperty("closeWin") as ScriptObject;
            Show.InvokeSelf();
        }

        private void txtPass_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                login();
            }
        }
    }
}
